import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xiaomishop/app/data/hot_good_model.dart';
import 'package:xiaomishop/app/net/HttManager.dart';
import 'package:xiaomishop/app/service/MyScreenExtention.dart';

import '../../../data/banner_model.dart';
import '../../../data/category_model.dart';
import '../../../net/Url.dart';

class MainController extends GetxController {
  //TODO: Implement MainController

  final count = 0.obs;
  RxDouble actionBarApla = 0.0.obs;
  double scrollSize = 30.hh;
  RxDouble actionMargin = RxDouble(170.ww);
  RxDouble miIconApla = 1.0.obs;
  RxList<BannerItem> mBannerItems = <BannerItem>[].obs;
  RxList<CategoryItem> mCategoryItems = <CategoryItem>[].obs;
  RxList<BannerItem> mHotGoogBannerItems = <BannerItem>[].obs;
  RxList<GoodItem> mHotGoodModelItems = <GoodItem>[].obs;
  RxList<GoodItem> mBottomGoodModelItems = <GoodItem>[].obs;
  RxInt itemCount = 2.obs;

  //listview的控制器
  final ScrollController scrollController = ScrollController();

  @override
  void onInit() {
    super.onInit();
    //设置监听器
    initListener();
    //
    initBannerDatas();
    initCatergotyDatas();
    initHotGoodsBannerDatas();
    initHotGoodsData();
    initBottomGoodsData();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void increment() => count.value++;

  void initListener() {
    //监听listview的滑动
    scrollController.addListener(() {
      var pixels = scrollController.position.pixels.ww;

      var percent = pixels / scrollSize;
      if (percent > 1) {
        percent = 1;
      }
      miIconApla.value = 1.0 - percent;
      actionBarApla.value = percent;
      actionMargin.value = 170.0.ww - percent * 140.ww;
      // print('百分比:${percent},pixels = ${pixels}');
      update();
    });
  }

  Rx<String> errorInfo = "".obs;

  /**
   * 初始化广告的数据，异步函数
   */
  void initBannerDatas() async {
    var rsp = await HttManager.instance.get(Url.API_FOCUS);
    if (rsp != null && rsp.data != null) {
      List<BannerItem> bannerItems = BannerModel.fromJson(rsp.data).result;
      mBannerItems.value = bannerItems;
    }
  }

  /**
   *初始化首页分类的数据
   */
  void initCatergotyDatas() async {
    var rsp = await HttManager.instance.get(Url.API_BESTCATE);
    if (rsp != null && rsp.data != null) {
      List<CategoryItem> categoryItems =
          CategoryModel.fromJson(rsp.data).result;
      if (categoryItems.isNotEmpty) {
        mCategoryItems.value = categoryItems;
        update();
      }
    }
  }

  void initHotGoodsBannerDatas() async {
    var map = {"position": 2};
    var rsp = await HttManager.instance.get(Url.API_FOCUS, map: map);
    if (rsp != null && rsp.data != null) {
      List<BannerItem> bannerItems = BannerModel.fromJson(rsp.data).result;
      if (bannerItems.isNotEmpty) {
        mHotGoogBannerItems.value = bannerItems;
        update();
      }
    }
  }

  void initHotGoodsData() async {
    var map = {"is_best": 1, "pageSize": 3};
    var rsp = await HttManager.instance.get(Url.API_PLIST, map: map);
    if (rsp != null && rsp.data != null) {
      List<GoodItem> hotGoods = HotGoodDataModel.fromJson(rsp.data).result;
      print('initHotGoodsData hotGoods：${hotGoods.length}');
      if (hotGoods.isNotEmpty) {
        mHotGoodModelItems.value = hotGoods;
        update();
      }
    }
  }

  void initBottomGoodsData() async {
    var map = {"is_best": 1};
    var rsp = await HttManager.instance.get(Url.API_PLIST, map: map);
    if (rsp != null && rsp.data != null) {
      List<GoodItem> hotGoods = HotGoodDataModel.fromJson(rsp.data).result;
      print('initBottomGoodsData hotGoods：${hotGoods.length}');
      if (hotGoods.isNotEmpty) {
        mBottomGoodModelItems.value = hotGoods;
        update();
      }
    }
  }
}
